import 'reflect-metadata';
import { DataSource } from 'typeorm';
//
//
interface dsType extends DataSource {
  [propName: string]: any;
}
export let AppDataSource: dsType = {} as dsType;

export async function DataSourceInit(): Promise<dsType> {
  try {
    if (Object.keys(AppDataSource).length) {
      return AppDataSource;
    }
    AppDataSource = new DataSource({
      type: 'mysql',
      host: 'localhost',
      port: 3306,
      username: 'root',
      password: 'root',
      database: 'test_new',
      entities: [],
      synchronize: true,
      logging: false,
    });
    await new Promise<string>((resolve, reject) => {
      AppDataSource.initialize()
        .then(() => {
          console.log('here you can start to work with your database');
          resolve('yes');
        })
        .catch((error) => reject(error));
    });
    return AppDataSource;
  } catch (error) {
    console.error('-DataSourceInit-', error);
    return Promise.reject(error);
  }
}

export const entities = [];
